package com.zzyl.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zzyl.base.PageResponse;
import com.zzyl.dto.NursingProjectDto;
import com.zzyl.entity.NursingProject;
import com.zzyl.enums.BasicEnum;
import com.zzyl.exception.BaseException;
import com.zzyl.mapper.NursingProjectMapper;
import com.zzyl.service.NursingProjectService;
import com.zzyl.vo.NursingProjectVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class NursingProjectServiceImpl implements NursingProjectService {

    @Autowired
    private NursingProjectMapper nursingProjectMapper;

    @Override
    public PageResponse<NursingProjectVo> findNursingProject(String name, Integer status, Integer page, Integer pageSize) {
        //分页查询
        PageHelper.startPage(page, pageSize);
        //查询数据
        Page<NursingProject> pageInfo = nursingProjectMapper.findNursingProject(name, status);
        // 封装护理项目关联护理计划数量(每一个护理项目关联几个护理计划)
        if (CollectionUtil.isEmpty(pageInfo.getResult())) {
            return PageResponse.of(pageInfo, NursingProjectVo.class);
        }
        //获取查询到的每一个护理项目id
        List<NursingProject> projectList = pageInfo.getResult();
        List<Long> ids = projectList.stream().map(nursingProject -> nursingProject.getId()).collect(Collectors.toList());
        //List<Long> ids = projectList.stream().map(NursingProject::getId).collect(Collectors.toList());
        //根据护理项目id查询每一个护理项目被引用的次数
        List<Map<String, Object>> refCount = nursingProjectMapper.findRefCount(ids);
        //如果没有任何一个护理项目被引用, 直接返回
        if (CollectionUtil.isEmpty(refCount)) {
            return PageResponse.of(pageInfo, NursingProjectVo.class);
        }
        //将护理项目引用数量集合转成Map集合 , 方便后期获取护理项目引用次数 Map<护理目id,关联数量>
        Map<Integer, Long> refCountMap = refCount.stream().collect(Collectors.toMap(map -> Integer.valueOf(map.get("projectId").toString()), map -> Long.valueOf(map.get("count").toString())));
        // 将分页数据Page<NursingProject>  转化为PageResponse<NursingProjectVo类型
        PageResponse<NursingProjectVo> projectVoPageResponse = PageResponse.of(pageInfo, NursingProjectVo.class);
        // 为分页数据总的每一个vo设置引用次数
        List<NursingProjectVo> nursingProjectVoList = projectVoPageResponse.getRecords();
        //通过流获取每一个vo对象
        nursingProjectVoList.stream().forEach(nursingProjectVo -> {
            //获取护理项目对应的引用数量
            Long count = refCountMap.getOrDefault(nursingProjectVo.getId().intValue(), 0L);
            nursingProjectVo.setCount(count.intValue());
        });

        return projectVoPageResponse;

        //将实体类转化为VO
        //        List<NursingProjectVo> nursingProjectVoList = projectList.stream().map(nursingProject -> {
        //            NursingProjectVo projectVo = BeanUtil.toBean(nursingProject, NursingProjectVo.class);
        //            //获取护理项目对应的引用数量
        //            Long count = refCountMap.getOrDefault(nursingProject.getId().intValue(), 0L);
        //            projectVo.setCount(count.intValue());
        //            return projectVo;
        //        }).collect(Collectors.toList());

        //return PageResponse.<NursingProjectVo>builder().page(pageInfo.getPageNum()).pages(Long.valueOf(pageInfo.getPages())).pageSize(pageInfo.getPageSize()).total(pageInfo.getTotal()).records(nursingProjectVoList).build();
    }

    @Override
    public void addNursingProject(NursingProjectDto nursingProjectDto) {
        NursingProject nursingProject = BeanUtil.toBean(nursingProjectDto, NursingProject.class);
        nursingProjectMapper.addNursingProject(nursingProject);
    }

    @Override
    public NursingProjectVo findById(Long id) {
        NursingProject nursingProject = nursingProjectMapper.findById(id);
        return BeanUtil.toBean(nursingProject,NursingProjectVo.class);
    }

    @Override
    public void modifyNursingProject(NursingProjectDto nursingProjectDto) {
        if (nursingProjectDto.getId()==null) {
            throw new BaseException(BasicEnum.PARAMETER_MISSING);
        }
        //如果护理项目被引用, 不能修改
        List<Map<String, Object>> refCount = nursingProjectMapper.findRefCount(List.of(nursingProjectDto.getId()));
        if (CollectionUtil.isNotEmpty(refCount)){
            throw new BaseException(BasicEnum.DATA_REFERENCED);
        }
        //修改数据库数据
        NursingProject nursingProject = BeanUtil.toBean(nursingProjectDto, NursingProject.class);
        nursingProjectMapper.modifyNursingProject(nursingProject);
    }
}
